<template>
  <div>
    <el-upload
      class="upload-demo"
      :action="action"
      :on-change="handleSuccess"
      :on-remove="handleRemove"
      :before-upload="beforeAvatarUpload"
      :accept="acceptStr"
      multiple
      :file-list="fileList"
      :headers="{ Authorization: token }"
    >
      <el-button size="small" type="primary">点击上传</el-button>
      <div slot="tip" class="el-upload__tip">
        只能上传jpg/png/word/pdf/ppt文件，且不超过500MB
      </div>
    </el-upload>
  </div>
</template>
<script>
import projectConfig from '@/core/config';
import { getToken } from '@/utils';
export default {
  props: {
    acceptStr: {
      type: String,
      default:
        '.doc,.docx,.xls,.xlsx,.jpg,.png,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
    },
    value: {
      type: Array,
      default: () => []
    }
  },
  model: {
    event: 'changeFile'
  },
  data () {
    return {
      token: getToken(),
      action: projectConfig.baseUrl + 'admin/knowledge/fileUpload',
      fileList: this.fileLists
    };
  },
  methods: {
    /**
     * 上传文件-成功
     */
    handleSuccess (file, fileList) {
      this.fileLists = fileList;
      console.log(fileList)
      this.$emit('changeFile', fileList)
    },
    /**
     * 上传文件-删除
     */
    handleRemove (file, fileList) {
      this.fileLists = fileList;
      this.$emit('changeFile', fileList)
    },
    /**
     * 上传图片限制条件
     */
    beforeAvatarUpload (file, fileList) {
      // const isJPG = file.type === 'image/jpeg'
      // const isLt200K = file.size / 1024 < 200
      // if (!isJPG) {
      //   this.$message.error('上传头像图片只能是 JPG 格式!')
      //   return false
      // }
      // if (!isLt200K) {
      //   this.$message.error('上传头像图片大小不能超过 200KB!')
      //   return false
      // }
      // return isJPG && isLt200K
    }
  }
};
</script>
<style lang="scss" scoped>
</style>
